
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>安装地理空间库 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../../search.html" />
    <link rel="next" title="安装 PostGIS" href="postgis.html" />
    <link rel="prev" title="GeoDjango 安装" href="index.html" />



 
<script src="../../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="GeoDjango 安装">previous</a>
     |
    <a href="../../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="postgis.html" title="安装 PostGIS">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-gis-install-geolibs">
            
  <div class="section" id="s-installing-geospatial-libraries">
<span id="installing-geospatial-libraries"></span><h1>安装地理空间库<a class="headerlink" href="#installing-geospatial-libraries" title="永久链接至标题">¶</a></h1>
<p>GeoDjango 使用和／或提供以下开源地理空间库的接口：</p>
<table class="docutils">
<colgroup>
<col width="18%" />
<col width="28%" />
<col width="25%" />
<col width="29%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">程序</th>
<th class="head">描述</th>
<th class="head">必须的</th>
<th class="head">支持的版本</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><a class="reference internal" href="../geos.html"><span class="doc">GEOS</span></a></td>
<td>几何引擎开源</td>
<td>是</td>
<td>3.9, 3.8, 3.7, 3.6, 3.5</td>
</tr>
<tr class="row-odd"><td><a class="reference external" href="https://proj.org/">PROJ</a></td>
<td>制图投影库</td>
<td>是（仅 PostgreSQL 和 SQLite）</td>
<td>7.x. 6.x, 5.x, 4.x</td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="../gdal.html"><span class="doc">GDAL</span></a></td>
<td>地理空间数据抽象库</td>
<td>是</td>
<td>3.2, 3.1, 3.0, 2.4, 2.3, 2.2, 2.1, 2.0</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="../geoip2.html"><span class="doc">GeoIP</span></a></td>
<td>基于 IP 的地理定位库</td>
<td>否</td>
<td>2</td>
</tr>
<tr class="row-even"><td><a class="reference external" href="https://postgis.net/">PostGIS</a></td>
<td>PostgreSQL 的空间扩展</td>
<td>是（仅 PostgreSQL）</td>
<td>3.0, 2.5, 2.4, 2.3</td>
</tr>
<tr class="row-odd"><td><a class="reference external" href="https://www.gaia-gis.it/gaia-sins/">SpatiaLite</a></td>
<td>SQLite 的空间扩展</td>
<td>是（仅 SQLite）</td>
<td>4.3</td>
</tr>
</tbody>
</table>
<p>请注意，这些库的老版本或最新版本可能也能与 GeoDjango 完全兼容。你的里程数可能会有所不同。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">GeoDjango 的 GEOS、GDAL 和 GeoIP 接口可以独立于 Django 使用。 换句话说，不需要数据库或配置文件 —— 像正常的那样从 <a class="reference internal" href="../index.html#module-django.contrib.gis" title="django.contrib.gis: Geographic Information System (GIS) extensions for Django"><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.gis</span></code></a> 导入它们。</p>
</div>
<p>在 Debian／Ubuntu 上，我们建议您安装以下软件包，它们将直接或通过依赖关系安装所需的地理空间库：</p>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>sudo apt-get install binutils libproj-dev gdal-bin
</pre></div>
</div>
<p>如果你使用的是 <a class="reference internal" href="index.html#macos"><span class="std std-ref">macOS</span></a> 或 <a class="reference internal" href="index.html#windows"><span class="std std-ref">Windows</span></a>，也请参考特定平台的说明。</p>
<div class="section" id="s-building-from-source">
<span id="s-build-from-source"></span><span id="building-from-source"></span><span id="build-from-source"></span><h2>从源码构建<a class="headerlink" href="#building-from-source" title="永久链接至标题">¶</a></h2>
<p>在 UNIX 和 GNU／Linux 系统上从源码安装时，请仔细按照安装说明，并按照给定的顺序安装库。 如果使用 MySQL 或 Oracle 作为空间数据库，则只需要 GEOS。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>在 Linux 平台上，可能需要在安装每个库后运行 <code class="docutils literal notranslate"><span class="pre">ldconfig</span></code> 命令。例如：</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span>$ sudo make install
$ sudo ldconfig
</pre></div>
</div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">macOS 用户必须安装 <a class="reference external" href="https://developer.apple.com/xcode/">Xcode</a> 才能从源码编译软件。</p>
</div>
<div class="section" id="s-geos">
<span id="s-geosbuild"></span><span id="geos"></span><span id="geosbuild"></span><h3>GEOS<a class="headerlink" href="#geos" title="永久链接至标题">¶</a></h3>
<p>GEOS 是一个用于执行几何运算的 C++ 库，是 GeoDjango 默认使用的内部几何表示法（它的背后是 “惰性” 的几何图形）。 具体来说，在 Python 中使用 ctypes 直接调用 C API 库（如 <code class="docutils literal notranslate"><span class="pre">libgeos_c.so</span></code>）。</p>
<p>首先，从 GEOS 网站下载 GEOS，并解压源文件：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ wget https://download.osgeo.org/geos/geos-X.Y.Z.tar.bz2
$ tar xjf geos-X.Y.Z.tar.bz2
</pre></div>
</div>
<p>接下来，切换到解压 GEOS 的目录下，运行配置脚本、编译、安装：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ cd geos-X.Y.Z
$ ./configure
$ make
$ sudo make install
$ cd ..
</pre></div>
</div>
<div class="section" id="s-troubleshooting">
<span id="troubleshooting"></span><h4>错误调试<a class="headerlink" href="#troubleshooting" title="永久链接至标题">¶</a></h4>
<div class="section" id="s-can-t-find-geos-library">
<span id="can-t-find-geos-library"></span><h5>找不到 GEOS 库<a class="headerlink" href="#can-t-find-geos-library" title="永久链接至标题">¶</a></h5>
<p>当 GeoDjango 找不到 GEOS 时，就会出现这个错误：</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>ImportError: Could not find the GEOS library (tried &quot;geos_c&quot;). Try setting GEOS_LIBRARY_PATH in your settings.
</pre></div>
</div>
<p>最常见的解决方案是正确配置你的 <a class="reference internal" href="index.html#libsettings"><span class="std std-ref">库环境配置</span></a> <em>或</em> 在你的配置中设置 <a class="reference internal" href="#geoslibrarypath"><span class="std std-ref">GEOS_LIBRARY_PATH</span></a>。</p>
<p>如果使用 GEOS 的二进制包（例如，在 Ubuntu 上），你可能需要 <a class="reference internal" href="index.html#binutils"><span class="std std-ref">安装 binutils</span></a>。</p>
</div>
<div class="section" id="s-geos-library-path">
<span id="s-geoslibrarypath"></span><span id="geos-library-path"></span><span id="geoslibrarypath"></span><h5><code class="docutils literal notranslate"><span class="pre">GEOS_LIBRARY_PATH</span></code><a class="headerlink" href="#geos-library-path" title="永久链接至标题">¶</a></h5>
<p>如果你的 GEOS 库在非标准位置，或者你不想修改系统的库路径，那么可以在你的 Django 配置文件中添加 <a class="reference internal" href="../geos.html#std:setting-GEOS_LIBRARY_PATH"><code class="xref std std-setting docutils literal notranslate"><span class="pre">GEOS_LIBRARY_PATH</span></code></a> 配置，并将 GEOS C 库的完整路径添加进去。 例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">GEOS_LIBRARY_PATH</span> <span class="o">=</span> <span class="s1">&#39;/home/bob/local/lib/libgeos_c.so&#39;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">这个配置必须是 <strong>C</strong> 共享库的 <em>完整</em> 路径；换句话说，你要使用 <code class="docutils literal notranslate"><span class="pre">libgeos_c.so</span></code>，而不是 <code class="docutils literal notranslate"><span class="pre">libgeos.so</span></code>。</p>
</div>
<p>另见 <a class="reference internal" href="../geos.html#geos-exceptions-in-logfile"><span class="std std-ref">我的日志中充满了与 GEOS 有关的错误</span></a>。</p>
</div>
</div>
</div>
<div class="section" id="s-proj4">
<span id="s-id3"></span><span id="proj4"></span><span id="id3"></span><h3>PROJ<a class="headerlink" href="#proj4" title="永久链接至标题">¶</a></h3>
<p><a class="reference external" href="https://proj.org/">PROJ</a> is a library for converting geospatial data to different coordinate
reference systems.</p>
<p>First, download the PROJ source code:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ wget https://download.osgeo.org/proj/proj-X.Y.Z.tar.gz
</pre></div>
</div>
<p>... and datum shifting files (download <code class="docutils literal notranslate"><span class="pre">proj-datumgrid-X.Y.tar.gz</span></code> for
PROJ &lt; 7.x) <a class="footnote-reference" href="#id9" id="id4">[1]</a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ wget https://download.osgeo.org/proj/proj-data-X.Y.tar.gz
</pre></div>
</div>
<p>Next, untar the source code archive, and extract the datum shifting files in the
<code class="docutils literal notranslate"><span class="pre">data</span></code> subdirectory (use <code class="docutils literal notranslate"><span class="pre">nad</span></code> subdirectory for PROJ &lt; 6.x). This must be
done <em>prior</em> to configuration:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ tar xzf proj-X.Y.Z.tar.gz
$ cd proj-X.Y.Z/data
$ tar xzf ../../proj-data-X.Y.tar.gz
$ cd ..
</pre></div>
</div>
<p>Finally, configure, make and install PROJ:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ ./configure
$ make
$ sudo make install
$ cd ..
</pre></div>
</div>
</div>
<div class="section" id="s-gdal">
<span id="s-gdalbuild"></span><span id="gdal"></span><span id="gdalbuild"></span><h3>GDAL<a class="headerlink" href="#gdal" title="永久链接至标题">¶</a></h3>
<p><a class="reference external" href="https://trac.osgeo.org/gdal/">GDAL</a> 是一个优秀的开源地理空间库，它支持读取大多数矢量和栅格空间数据格式。目前 GeoDjango 只支持 <span class="xref std std-doc">GDAL 的矢量数据</span> 的功能 <a class="footnote-reference" href="#id10" id="id5">[2]</a> 。 在构建 GDAL 之前，应该安装 <a class="reference internal" href="#geosbuild"><span class="std std-ref">GEOS</span></a> 和 <a class="reference internal" href="#proj4"><span class="std std-ref">PROJ</span></a>。</p>
<p>首先下载最新的 GDAL 版本，解压压缩包：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ wget https://download.osgeo.org/gdal/X.Y.Z/gdal-X.Y.Z.tar.gz
$ tar xzf gdal-X.Y.Z.tar.gz
$ cd gdal-X.Y.Z
</pre></div>
</div>
<p>配置、编译和安装：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ ./configure
$ make # Go get some coffee, this takes a while.
$ sudo make install
$ cd ..
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">因为 GeoDjango 有自己的 Python 接口，所以前面的说明并没有构建 GDAL 自己的 Python 绑定。当运行 <code class="docutils literal notranslate"><span class="pre">configure</span></code> 时，可以通过添加 <code class="docutils literal notranslate"><span class="pre">--with-python</span></code> 标志来构建绑定。关于 GDAL 的绑定的更多信息，请参见 <a class="reference external" href="https://trac.osgeo.org/gdal/wiki/GdalOgrInPython">GDAL/OGR In Python</a> 。</p>
</div>
<p>如果你有任何问题，请参见下面的故障排除部分，以获得建议和解决方案。</p>
<div class="section" id="s-gdaltrouble">
<span id="s-id8"></span><span id="gdaltrouble"></span><span id="id8"></span><h4>错误调试<a class="headerlink" href="#gdaltrouble" title="永久链接至标题">¶</a></h4>
<div class="section" id="s-can-t-find-gdal-library">
<span id="can-t-find-gdal-library"></span><h5>找不到 GDAL 库<a class="headerlink" href="#can-t-find-gdal-library" title="永久链接至标题">¶</a></h5>
<p>当 GeoDjango 找不到 GDAL 库时，在你的配置中设置你的 <a class="reference internal" href="index.html#libsettings"><span class="std std-ref">库环境配置</span></a> <em>或</em> <a class="reference internal" href="#gdallibrarypath"><span class="std std-ref">GDAL_LIBRARY_PATH</span></a>。</p>
</div>
<div class="section" id="s-gdal-library-path">
<span id="s-gdallibrarypath"></span><span id="gdal-library-path"></span><span id="gdallibrarypath"></span><h5><code class="docutils literal notranslate"><span class="pre">GDAL_LIBRARY_PATH</span></code><a class="headerlink" href="#gdal-library-path" title="永久链接至标题">¶</a></h5>
<p>如果你的 GDAL 库在非标准位置，或者你不想修改系统的库路径，那么可以在你的 Django 配置文件中添加 <a class="reference internal" href="../gdal.html#std:setting-GDAL_LIBRARY_PATH"><code class="xref std std-setting docutils literal notranslate"><span class="pre">GDAL_LIBRARY_PATH</span></code></a> 设置，并添加 GDAL 库的完整路径。 例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">GDAL_LIBRARY_PATH</span> <span class="o">=</span> <span class="s1">&#39;/home/sue/local/lib/libgdal.so&#39;</span>
</pre></div>
</div>
<p class="rubric">脚注</p>
<table class="docutils footnote" frame="void" id="id9" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id4">[1]</a></td><td>The datum shifting files are needed for converting data to and from
certain projections.
For example, the PROJ string for the <a class="reference external" href="https://spatialreference.org/ref/sr-org/6864/prj/">Google projection (900913 or 3857)</a> requires the
<code class="docutils literal notranslate"><span class="pre">null</span></code> grid file only included in the extra datum shifting files.
It is easier to install the shifting files now, then to have debug a
problem caused by their absence later.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="id10" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id5">[2]</a></td><td>具体来说，GeoDjango 提供了对 <a class="reference external" href="https://gdal.org/user/vector_data_model.html">OGR</a> 库的支持，这是 GDAL 的一个组件。</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">安装地理空间库</a><ul>
<li><a class="reference internal" href="#building-from-source">从源码构建</a><ul>
<li><a class="reference internal" href="#geos">GEOS</a><ul>
<li><a class="reference internal" href="#troubleshooting">错误调试</a><ul>
<li><a class="reference internal" href="#can-t-find-geos-library">找不到 GEOS 库</a></li>
<li><a class="reference internal" href="#geos-library-path"><code class="docutils literal notranslate"><span class="pre">GEOS_LIBRARY_PATH</span></code></a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#proj4">PROJ</a></li>
<li><a class="reference internal" href="#gdal">GDAL</a><ul>
<li><a class="reference internal" href="#gdaltrouble">错误调试</a><ul>
<li><a class="reference internal" href="#can-t-find-gdal-library">找不到 GDAL 库</a></li>
<li><a class="reference internal" href="#gdal-library-path"><code class="docutils literal notranslate"><span class="pre">GDAL_LIBRARY_PATH</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="index.html"
                        title="上一章">GeoDjango 安装</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="postgis.html"
                        title="下一章">安装 PostGIS</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../../_sources/ref/contrib/gis/install/geolibs.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="GeoDjango 安装">previous</a>
     |
    <a href="../../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="postgis.html" title="安装 PostGIS">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>